package crawler.handler;

import crawler.common.PostData;
import crawler.common.SiteData;
import crawler.impl.PostCrawlerImpl;
import crawler.itf.PostParserItf;
import crawler.model.BongDaModel;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;

public class BongDaCrawlHandler {
    private static final Logger logger = Logger.getLogger(BongDaCrawlHandler.class);

    public static final int WORDER_COUNT = 1;

    private static Worker[] workers = null;
    
    public static void init(List<SiteData> site) {
        try {
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                @Override
                public void run() {
                    if (workers != null) {
                        for (Worker worker : workers) {
                            try {
                                worker.join();
                            }catch (InterruptedException ex) {
                                logger.error("InterruptedException: " + ex.getMessage(), ex);
                            }
                        }
                    }
                }
            }));

            workers = new Worker[site.size()];
            for (int i = 0; i < workers.length; i++) {
                workers[i] = new Worker("Worker " + i, site.get(i));
                workers[i].start();
            }
        } catch (Exception ex) {
            logger.error("WorkerHandler.init: " + ex, ex);
        }
    }
    private static class Worker extends Thread {
        private SiteData site = null;

        public Worker(String name, SiteData site) {
            super(name);
            this.site = site;
        }

        @SuppressWarnings("SleepWhileInLoop")
        @Override
        public void run() {
            while (true) {
                try {
                    int newsCount = 0;
                    PostParserItf parser = site.getParser();
                    BongDaModel bongdaModel = new BongDaModel();
                    PostCrawlerImpl postCrawler = new PostCrawlerImpl();
                    Document listTitle = postCrawler.getDocument(site.getUrl());
                    if(listTitle == null) continue;
                    List<PostData> lstPostData = new ArrayList<>();
                    try{
                        lstPostData = parser.getListTitle(listTitle);
                    }catch (Exception ex){
                        logger.error("Exception: " + ex.getMessage(), ex);
                    }
                    for (PostData postData : lstPostData) {
                        if(null == postData)
                            continue;
                        boolean exists = bongdaModel.checkPostExists(postData.getUrl());
                        if(!exists){
                            String thumb = postData.getThumb();
                            Document detail = postCrawler.getDocument(postData.getUrl());
                            if(detail == null) continue;
                            try{
                                postData = parser.getDetail(detail);
                            }catch (Exception ex){
                                logger.error("Exception: " + ex.getMessage(), ex);
                            }
                            if(postData!=null){
                                postData.setThumb(thumb);
                                bongdaModel.addNewPost(postData);
                                newsCount ++;
                            }
                        }
                    }
                    logger.info("Crawl xong " + parser.getClass().getSimpleName() + " luc "  + new Date() +" - them "+ newsCount +" bai viet moi");
                    Thread.sleep(1 * 3600 * 1000);
                } catch (Exception ex) {
                    logger.error("Exception: " + ex.getMessage(), ex);
                    break;
                }
            }
        }

    }
    public static void stopAll(){
       
    }
}
